﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPFLib.Misc;
using System.Collections.ObjectModel;
using WPFLib;
using System.Windows.Data;
using WPFLib.Navigation;

namespace NavigationDemo
{
    public class MainController : PropertyChangedHelper
    {
        public INavigationManager Navigation
        {
            get
            {
                return NavigationManager.Instance;
            }
        }

        public void AddController(object controller)
        {
            var type = controllers.GetType();
            if (this.controllers.Any(c => c.GetType() == type))
            {
                return;
            }
            this.controllers.Add(controller);
            var view = CollectionViewSource.GetDefaultView(Controllers);
            view.MoveCurrentTo(controller);
        }

        #region OpenControl1Command
        public static readonly string OpenControl1CommandProperty = "OpenControl1Command";
        private DelegateCommand _OpenControl1Command;

        public DelegateCommand OpenControl1Command
        {
            get
            {
                if (_OpenControl1Command == null)
                {
                    _OpenControl1Command = new DelegateCommand(OnOpenControl1CommandExecuted, OnOpenControl1CommandCanExecute);
                }
                return _OpenControl1Command;
            }
        }

        protected virtual bool OnOpenControl1CommandCanExecute()
        {
            return true;
        }

        protected virtual void OnOpenControl1CommandExecuted()
        {
            AddController(new Controller1());
        }
        #endregion

        #region OpenControl2Command
        public static readonly string OpenControl2CommandProperty = "OpenControl2Command";
        private DelegateCommand _OpenControl2Command;

        public DelegateCommand OpenControl2Command
        {
            get
            {
                if (_OpenControl2Command == null)
                {
                    _OpenControl2Command = new DelegateCommand(OnOpenControl2CommandExecuted, OnOpenControl2CommandCanExecute);
                }
                return _OpenControl2Command;
            }
        }

        protected virtual bool OnOpenControl2CommandCanExecute()
        {
            return true;
        }

        protected virtual void OnOpenControl2CommandExecuted()
        {
            AddController(new Controller2());
        }
        #endregion

        public MainController()
        {
            controllers = new ObservableCollection<object>();
            Controllers = new ReadOnlyObservableCollection<object>(controllers);
        }

        private ObservableCollection<object> controllers;

        public ReadOnlyObservableCollection<object> Controllers
        {
            get;
            private set;
        }
    }
}
